package com.huishen.edrivenew.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import butterknife.InjectView;

import com.huishen.edrivenew.AppContext;
import com.huishen.edrivenew.R;
import com.huishen.edrivenew.bean.CoaLessonInfoBean;
import com.huishen.edrivenew.bean.CoachBean;
import com.huishen.edrivenew.bean.CoachListBean;
import com.huishen.edrivenew.bean.LessInfoBean;
import com.huishen.edrivenew.bean.LessonTimeBean;
import com.huishen.edrivenew.bean.LessonlistSchoolBean;
import com.huishen.edrivenew.bean.ListLessonTimeBean;
import com.huishen.edrivenew.util.TLog;
import com.huishen.edrivenew.widget.CircleImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学时  新
 *
 */
public class ClassListSchoolApdater extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater = null;
    private ClassSchoolListener listener;

    private LessonlistSchoolBean classList;
    private List<LessInfoBean> rows = new ArrayList<LessInfoBean>();
    
    private int index = 0;

    public ClassListSchoolApdater(Context ctx, LessonlistSchoolBean coachList,
                                  ClassSchoolListener listener) {
        mContext = ctx;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.listener = listener;
        this.classList = coachList;
        index = AppContext.getInstance().classActivityType;
        
        setData();
    }



    public void setData() {
        this.rows.clear();
        this.rows.addAll(classList.rows);
    }
    

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        GroupViewHolder group = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_schoolclass_sub, null);
            group = new GroupViewHolder();
            group.sc_date = (TextView) convertView.findViewById(R.id.sc_date);
            group.sc_number = (TextView) convertView.findViewById(R.id.sc_number);
            group.sc_statu_tv = (TextView) convertView.findViewById(R.id.sc_statu_tv);
            group.sc_detial = (TextView) convertView.findViewById(R.id.sc_detial);
            group.sc_statu_btn = (Button) convertView.findViewById(R.id.sc_statu_btn);
            group.sc_btn_layout = convertView.findViewById(R.id.sc_statu_btn_layout);

            convertView.setTag(group);
        } else {
            group = (GroupViewHolder) convertView.getTag();
        }
        
        // ----------------添加列表数据------------------
        final LessInfoBean gBean = rows.get(position);
        
        group.sc_date.setText(gBean.lessonDate);
        group.sc_number.setText(gBean.timeFrame);
        
        if(gBean.status == 1){ //已约 可以取消
            group.sc_statu_btn.setText("取消");
            group.sc_btn_layout.setVisibility(View.VISIBLE);
            group.sc_statu_tv.setVisibility(View.GONE);
            group.sc_detial.setTextColor(AppContext.resources().getColor(R.color.tv_gray_0));
            
        }else if(gBean.status == 2){ //可以点击评价
            group.sc_statu_btn.setText("评价");
            group.sc_btn_layout.setVisibility(View.VISIBLE);
            group.sc_statu_tv.setVisibility(View.GONE);
            group.sc_detial.setTextColor(AppContext.resources().getColor(R.color.main_color));
            
        }else if(gBean.status == 3){ //已取消
            group.sc_statu_tv.setText("已取消");
            group.sc_btn_layout.setVisibility(View.GONE);
            group.sc_statu_tv.setVisibility(View.VISIBLE);
            group.sc_detial.setTextColor(AppContext.resources().getColor(R.color.tv_gray_0));
            
        }else{//已评价
            group.sc_statu_tv.setText("已评价");
            group.sc_btn_layout.setVisibility(View.GONE);
            group.sc_statu_tv.setVisibility(View.VISIBLE);
            group.sc_detial.setTextColor(AppContext.resources().getColor(R.color.main_color));
        }
        
        if(index == 1) {
            group.sc_detial.setVisibility(View.GONE);
        } else if(index == 2) {
            // 个人中心 学时统计页面
            group.sc_detial.setVisibility(View.GONE);
            if(gBean.status == 3){ //已取消
                group.sc_statu_tv.setText("已取消");
                group.sc_btn_layout.setVisibility(View.GONE);
                group.sc_statu_tv.setVisibility(View.VISIBLE);
                group.sc_detial.setTextColor(AppContext.resources().getColor(R.color.tv_gray_0));
            }else{//已评价
                group.sc_statu_tv.setText("已评价");
                group.sc_btn_layout.setVisibility(View.GONE);
                group.sc_statu_tv.setVisibility(View.VISIBLE);
                group.sc_detial.setTextColor(AppContext.resources().getColor(R.color.main_color));
            }
        } 
        
        OnClickListener onClickListener = new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                if(arg0.getId() == R.id.sc_statu_btn) {
                    listener.setOnClick(gBean.status, position, gBean.lessonInfoId);
                } else if(arg0.getId() == R.id.sc_detial) {
                    if(gBean.status== 1|| gBean.status==3) {
                        // 可点击状态
                        return;
                    }
                    listener.setOnClick(3, position, gBean.lessonInfoId);
                }
            }
        };
        
        group.sc_statu_btn.setOnClickListener(onClickListener);
        group.sc_detial.setOnClickListener(onClickListener);
        
        return convertView;
    }



    private class GroupViewHolder {
        TextView sc_date;
        TextView sc_number;
        TextView sc_statu_tv;
        TextView sc_detial;
        Button sc_statu_btn;
        View sc_btn_layout;
    }





    @Override
    public int getCount() {
        return rows.size();
    }



    @Override
    public Object getItem(int position) {
        return rows.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


}
